928. The sum of the largest and the smallest

 

The array of integers is given. Find the sum of the smallest and the largest elements of the array.

 

Input. The first line contains the number of elements n (n ≤ 100) in the array. The second line contains n elements of the array. Each number in the array does not exceed 100 in absolute value.

 

Output. Print the sum of the smallest and the largest elements of the array.

 

Sample input

Sample output

4

1 2 3 4

5

 

 

SOLUTION

arrays

 

 

Algorithm analysis

This problem can be solved:

·        using a loop to compute the minimum and maximum elements;

·        using an array;

 

Algorithm implementation

Read the number of integers n in the array.

 

scanf("%d",&n);

 

Compute the smallest and largest numbers in the variables min and max. Initialize the variables.

 

min = 100; max = -100;

 

Compute the minimum and maximum elements in the input array. Read and process the data on the fly.

 

for(i = 0; i < n; i++)

{

  scanf("%d",&a);

  if (a < min) min = a;

  if (a > max) max = a;

}

 

Print the sum of the smallest and largest elements.

 

printf("%d\n",min + max);

 

Algorithm implementation array

Declare an array m to store the input sequence of numbers.

 

int m[100];

 

Read the input data.

 

scanf("%d", &n);

for (i = 0; i < n; i++)

  scanf("%d", &m[i]);

 

Compute the minimum min and maximum max elements in the array.

 

mn = 100; mx = -100;

for (i = 0; i < n; i++)

{

  if (m[i] < mn) mn = m[i];

  if (m[i] > mx) mx = m[i];

}

 

Print the answer.

 

printf("%d\n", mn + mx);

 

Algorithm implementation dynamic array

 

#include <stdio.h>

 

int i, n, min, max;

int *m;

 

int main(void)

{

  scanf("%d",&n);

  m = new int[n];

 

  for(i = 0; i < n; i++)

    scanf("%d",&m[i]);

 

  min = max = m[0];

  //min = 100; max = -100;

 

  for(i = 0; i < n; i++)

  {

    if (m[i] < min) min = m[i];

    if (m[i] > max) max = m[i];

  }

 

  printf("%d\n",min + max);

  delete[] m;

 

  return 0;

}

 

Algorithm implementation STL

Declare an array m to store the input sequence of numbers.

 

int m[101];

 

Read the input data.

 

scanf("%d",&n);

for(i = 0; i < n; i++)

  scanf("%d",&m[i]);

 

Compute the sum of the minimum and maximum elements in the array.

 

res = *min_element(m,m+n) + *max_element(m,m+n);

 

Print the answer.

 

printf("%d\n",res);

 

Algorithm implementation – function as a parameter

 

#include <stdio.h>

 

int i, n, res, *m;

 

int GetMin(int *mas, int n)

{

  int i, min = 10000000;

  for(i = 0; i < n; i++)

    if (mas[i] < min) min = mas[i];

  return min;

}

 

int GetMax(int *mas, int n)

{

  int i, max = -10000000;

  for(i = 0; i < n; i++)

    if (mas[i] > max) max = mas[i];

  return max;

}

 

int main(void)

{

  scanf("%d",&n);

  m = new int[n];

  for(i = 0; i < n; i++)

    scanf("%d",&m[i]);

  res = GetMin(m,n) + GetMax(m,n);

  printf("%d\n",res);

  delete[] m;

  return 0;

}

 

Algorithm implementation – classes

 

#include <stdio.h>

 

int i, n, res, *m;

 

class Array

{

public:

   int *m;

   int len;

   Array(int n = 1)

   {

     len = n;

     m = new int[n];

   }

   int& operator[](int i)

   {

     return m[i];

   }

   int GetMax()

   {

     int i, max = -100000;

     for(i = 0; i < len; i++)

       if (m[i] > max) max = m[i];

     return max;

   }

   int GetMin()

   {

     int i, min = 100000;

     for(i = 0; i < len; i++)

       if (m[i] < min) min = m[i];

     return min;

   }

};

 

int main(void)

{

  scanf("%d",&n);

  Array m(n);

  for(i = 0; i < n; i++)

    scanf("%d",&m[i]);

  res = m.GetMin() + m.GetMax();

  printf("%d\n",res);

  return 0;

}

 

Java implementation

 

import java.util.*;

 

public class Main

{

  public static void main(String[] args)

  {

    Scanner con = new Scanner(System.in);

    int n = con.nextInt();

    int min = con.nextInt(), max = min;

    for(int i = 1; i < n; i++)

    {

      int val = con.nextInt();

      if (val < min) min = val;

      if (val > max) max = val;

    }

    System.out.println(min + max);

    con.close();

  }

}

 

Java implementation – initialization of min and max

 

import java.util.*;

 

public class Main

{

  public static void main(String[] args)

  {

    Scanner con = new Scanner(System.in);

    int n = con.nextInt();

    int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE;

    for(int i = 1; i <= n; i++)

    {

      int val = con.nextInt();

      if (val < min) min = val;

      if (val > max) max = val;

    }

    System.out.println(min + max);

    con.close();

  }

}

 

Java implementation – ArrayList

 

import java.util.*;

 

public class Main

{

  public static void main(String[] args)

  {

    Scanner con = new Scanner(System.in);

    int n = con.nextInt();

 

    ArrayList<Integer> m = new ArrayList<Integer>();

    for(int i = 0; i < n; i++)

      m.add(con.nextInt());

   

    int min = Integer.MAX_VALUE;

    int max = Integer.MIN_VALUE;

    for(int i = 0; i < n; i++)

    {

      if (m.get(i) < min) min = m.get(i);

      if (m.get(i) > max) max = m.get(i);

    }

   

    System.out.println(min + max);

    con.close();

  }

}

 

Java implementation classes

 

import java.util.*;

 

class Array

{

  int n;

  int m[];

 

  Array(int n)

  {

    this.n = n;

    m = new int[n];

  }

 

  void Set(int i, int val)

  {

    m[i] = val;

  }

 

  int GetMax()

  {

    int max = Integer.MIN_VALUE;

    for(int i = 0; i < n; i++)

      if (m[i] > max) max = m[i];

    return max;

  }

 

  int GetMin()

  {

    int min = Integer.MAX_VALUE;

    for(int i = 0; i < n; i++)

      if (m[i] < min) min = m[i];

    return min;

  } 

}

 

public class Main

{

  public static void main(String[] args)

  {

    Scanner con = new Scanner(System.in);

    int n = con.nextInt();

    Array a = new Array(n);

   

    for(int i = 0; i < n; i++)

    {

      int val = con.nextInt();

      a.Set(i,val);

    }

   

    System.out.println(a.GetMin() + a.GetMax());

    con.close();

  }

}

 

Python implementation

Read the input data.

 

n = int(input())

m = list(map(int,input().split()))

 

Compute the smallest and largest numbers in the variables min and max. Initialize the variables.

 

min = max = m[0]

 

Compute the minimum min and maximum max elements in the array.

 

for v in m:

  if v < min: min = v

  if v > max: max = v

 

Compute and print the sum of the smallest and largest elements.

 

res = min + max

print (res)

 

Python implementation – min and max functions

Read the input data.

 

n = int(input())

m = list(map(int,input().split()))

 

Print the sum of the smallest and largest elements.

 

print (min(m) + max(m))